package pl.kosmowska.ewelina.client.views.impl;

import java.util.Date;

import pl.kosmowska.ewelina.client.views.ChartView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.LegendPosition;
import com.google.gwt.visualization.client.visualizations.corechart.AreaChart;
import com.google.gwt.visualization.client.visualizations.corechart.BarChart;
import com.google.gwt.visualization.client.visualizations.corechart.ColumnChart;
import com.google.gwt.visualization.client.visualizations.corechart.Options;
import com.google.gwt.visualization.client.visualizations.corechart.PieChart;

public class ChartViewImpl extends Composite implements ChartView {

	private static ChartViewImplUiBinder uiBinder = GWT
			.create(ChartViewImplUiBinder.class);

	interface ChartViewImplUiBinder extends UiBinder<Widget, ChartViewImpl> {
	}


	@UiField
	SimplePanel outcomeCategoryChartPanel;
	@UiField
	SimplePanel incomeCategoryChartPanel;
	@UiField
	SimplePanel incomeVsOutcomeChartPanel;
	@UiField
	SimplePanel balanceChartPanel;
	@UiField
	SimplePanel outcomeRepetedChartPanel;
	@UiField
	SimplePanel incomeRepetedChartPanel;
	@UiField
	SimplePanel finalBalanceChartPanel;
	@UiField
	SimplePanel startDatePanel;
	@UiField
	SimplePanel endDatePanel;
	@UiField
	HorizontalPanel searchPanel;


	private boolean alreadyInit;
	private Presenter presenter;

	private DateBox startDateBox;
	private DateBox endDateBox;

	public ChartViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		startDateBox = new DateBox();
		endDateBox = new DateBox();
		startDatePanel.setWidget(getDateBox(startDateBox, "Od: "));
		endDatePanel.setWidget(getDateBox(endDateBox, "Do: "));
	}


	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	public HorizontalPanel getDateBox(DateBox dateBox, String name){
		// Create a DateBox
		DateTimeFormat dateFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.YEAR_MONTH_DAY);
		//    dateBox = new DateBox();
		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

		// Combine the widgets into a panel and return them
		HorizontalPanel vPanel = new HorizontalPanel();
		vPanel.add(new HTML(name));
		vPanel.add(dateBox);
		return vPanel;
	}

	public void loadOutcomeCategoryChart(final AbstractDataTable dataTable){
		PieChart pie = new PieChart(dataTable, createOutcomeCateogryChartOptions());
		outcomeCategoryChartPanel.setWidget(pie);
	}

	public void loadIncomeCategoryChart(final AbstractDataTable dataTable){
		PieChart pie = new PieChart(dataTable, createIncomeCateogryChartOptions());
		incomeCategoryChartPanel.setWidget(pie);
	}

	public void loadIncomevsOutcomeChart(final AbstractDataTable dataTable){
		ColumnChart columnChart = new ColumnChart(dataTable, createIncomeVsOutcomeChartOptions());
		incomeVsOutcomeChartPanel.setWidget(columnChart);
}

	public void loadFinalBalanceChart(final AbstractDataTable dataTable, final boolean balancePositive){
		ColumnChart columnChart = new ColumnChart(dataTable, createFinalBalanceChartOptions(balancePositive));
		finalBalanceChartPanel.setWidget(columnChart);
	}


	public void loadOutcomeRepeatedChart(final AbstractDataTable dataTable){
		BarChart columnChart = new BarChart(dataTable, createOutcomeRepeatedChartOptions());
		outcomeRepetedChartPanel.setWidget(columnChart);
	}

	public void loadIncomeRepeatedChart(final AbstractDataTable dataTable){
		BarChart columnChart = new BarChart(dataTable, createIncomeRepeatedChartOptions());
		incomeRepetedChartPanel.setWidget(columnChart);
	}

	public void loadBalanceChart(final AbstractDataTable dataTable){
		AreaChart areaChart = new AreaChart(dataTable, createBalanceChartOptions());
		balanceChartPanel.setWidget(areaChart);
	}

	protected Options createFinalBalanceChartOptions(boolean balancePositive) {
		Options options = Options.create();
		options.setWidth(400);
		options.setHeight(240);
		options.setTitle("Saldo");
		if(balancePositive)
			options.setColors("green");
		else
			options.setColors("red");
		options.setLegend(LegendPosition.BOTTOM);
		return options;
	}

	protected Options createOutcomeRepeatedChartOptions() {
		Options options = Options.create();
		options.setWidth(400);
		options.setHeight(240);
		options.setLegend(LegendPosition.BOTTOM);
		options.setTitle("Wydatki sta\u0142e i pojedy\u0144cze");
		return options;
	}

	protected Options createIncomeRepeatedChartOptions() {
		Options options = Options.create();
		options.setWidth(400);
		options.setHeight(240);
		options.setLegend(LegendPosition.BOTTOM);
		options.setTitle("Przychody sta\u0142e i pojedy\u0144cze");
		return options;
	}

	protected Options createBalanceChartOptions() {
		Options options = Options.create();
		options.setWidth(1200);
		options.setHeight(240);
		options.setTitle("Rozk\u0142ad przychod\u00F3w i wydatk\u00F3w");
		options.setLegend(LegendPosition.BOTTOM);

		return options;
	}

	private Options createOutcomeCateogryChartOptions() {
		com.google.gwt.visualization.client.visualizations.corechart.PieChart.PieOptions options = com.google.gwt.visualization.client.visualizations.corechart.PieChart.PieOptions.create();
		options.setWidth(400);
		options.setHeight(240);
		options.set3D(true);
		options.setTitle("Rozklad wydatkow ze wzgledu na kategorie");
		options.setLegend(LegendPosition.BOTTOM);
		return options;
	}
	private Options createIncomeCateogryChartOptions() {
		com.google.gwt.visualization.client.visualizations.corechart.PieChart.PieOptions options = com.google.gwt.visualization.client.visualizations.corechart.PieChart.PieOptions.create();
		options.setWidth(400);
		options.setHeight(240);
		options.set3D(true);
		options.setTitle("Rozk\u0142ad przychod\u00F3w ze wzgl\u0119du na kategorie");
		options.setLegend(LegendPosition.BOTTOM);
		return options;
	}

	private Options createIncomeVsOutcomeChartOptions() {
		Options options = Options.create();
		options.setWidth(400);
		options.setHeight(240);
		//options.set3D(true);
		options.setTitle("Wydatki i przychody");
		options.setLegend(LegendPosition.BOTTOM);
		return options;
	}


	@Override
	public void initOutcomeCategoryChart(AbstractDataTable dataTable) {
		loadOutcomeCategoryChart(dataTable);
	}

	@Override
	public void initIncomeCategoryChart(AbstractDataTable dataTable) {
		loadIncomeCategoryChart(dataTable);

	}

	@Override
	public void initBalanceChart(AbstractDataTable dataTable) {
		loadBalanceChart(dataTable);

	}

	@Override
	public void initOutcomeVsIncomeChart(AbstractDataTable dataTable) {
		loadIncomevsOutcomeChart(dataTable);

	}

	@Override
	public void initOutcomeRepeatedChart(AbstractDataTable dataTable) {
		loadOutcomeRepeatedChart(dataTable);

	}

	@Override
	public void initIncomeRepeatedChart(AbstractDataTable dataTable) {
		loadIncomeRepeatedChart(dataTable);

	}


	@Override
	public void initFinalBalanceChart(AbstractDataTable dataTable,
			boolean balancePositive) {
		loadFinalBalanceChart(dataTable, balancePositive);

	}

	@UiHandler("loadOutcomes")
	void onLoadOutcomesClick(ClickEvent event) {
		Date startDate =startDateBox.getValue();
		Date endDate =endDateBox.getValue();
		if(startDate==null || endDate==null){
			showMessage("Ustaw daty");
			return;
		}else if (startDate.getTime()> endDate.getTime()){
			showMessage("Błędnie ustawione daty wyszukiwania");
			return;
		}

		java.sql.Date sqlDate1 = new java.sql.Date(startDate.getTime());
		java.sql.Date sqlDate2 = new java.sql.Date(endDate.getTime());

		presenter.loadDateForCharts(sqlDate1, sqlDate2);
	}

	@Override
	public void showMessage(String message) {
		MessageDialogBox messageDialogBox = new MessageDialogBox(message);
		messageDialogBox.center();
		messageDialogBox.show();

	}


	@UiHandler("searchRBtnStandard")
	void onSearchRBtnStandardClick(ClickEvent event) {
		searchPanel.setVisible(false);
		presenter.loadDataForCharts();
	}

	@UiHandler("searchRBtnAdvanced")
	void onSearchRBtnAdvancedClick(ClickEvent event) {
		searchPanel.setVisible(true);
	}

	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}

	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}





}
